﻿////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) Oleg Sivokon email: olegsivokon@gmail.com
//  
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//  Or visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
//
////////////////////////////////////////////////////////////////////////////////

package org.wvxvws.gui.renderers
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	
	/**
	* ImageRenderer class.
	* @author wvxvw
	* @langVersion 3.0
	* @playerVersion 10.0.12.36
	*/
	public class ImageRenderer extends Sprite implements IRenderer
	{
		//--------------------------------------------------------------------------
		//
		//  Public properties
		//
		//--------------------------------------------------------------------------
		
		public function get data():XML { return this._data; }
		
		public function set data(value:XML):void 
		{
			if (isValid && this._data === value) return;
			this._data = value;
			var lastSRC:String = this._src;
			this._src = this._data.@src;
			if (!_src) throw new Error("Must define @src attribute");
			if (this._image.content && lastSRC != this._src) this._image.unload();
			else this._image.load(new URLRequest(this._src));
		}
		
		/**
		 * Returns <code>false</code> if the data is out of sync with the source.
		 * Otherwise returns <code>true</code>
		 */
		public function get isValid():Boolean
		{
			if (!this._data) return false;
			return this._src == this._data.@src;
		}
		
		public function set labelFunction(value:Function):void
		{
			// TODO:
		}
		
		public function set labelField(value:String):String
		{
			// TODO:
		}
		
		//--------------------------------------------------------------------------
		//
		//  Protected properties
		//
		//--------------------------------------------------------------------------
		
		protected var _image:Loader = new Loader();
		protected var _imageBitmap:Bitmap;
		protected var _imageData:BitmapData;
		protected var _data:XML;
		protected var _src:String;
		protected var _document:Object;
		protected var _id:String;
		
		//--------------------------------------------------------------------------
		//
		//  Private properties
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function ImageRenderer() 
		{
			super();
			super.graphics.beginFill(0xFFFFFF, 0);
			super.graphics.drawRect(0, 0, 100, 100);
			super.graphics.endFill();
			this._image.contentLoaderInfo.addEventListener(
									Event.COMPLETE, this.completeHandler);
			this._image.contentLoaderInfo.addEventListener(
									IOErrorEvent.IO_ERROR, super.dispatchEvent);
			this._image.contentLoaderInfo.addEventListener(
						SecurityErrorEvent.SECURITY_ERROR, super.dispatchEvent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Public methods
		//
		//--------------------------------------------------------------------------
		
		/* INTERFACE org.wvxvws.gui.IRenderer */
		/**
		 * Necessary implementation of IMXMLObject. Otherwise we cannot live in the 
		 * MXML file.
		 * @param	document	The object which called this methid.
		 * @param	id			The ID either assigned through MXML or autogenerated.
		 */
		public function initialized(document:Object, id:String):void
		{
			this._document = document;
			this._id = id;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Protected methods
		//
		//--------------------------------------------------------------------------
		
		protected function completeHandler(event:Event):void 
		{
			if (this._imageData) this._imageData.dispose();
			this._image.width = super.width;
			this._image.height = super.height;
			this._image.scaleX = Math.min(this._image.scaleX, this._image.scaleY);
			this._image.scaleY = this._image.scaleX;
			this._imageData = new BitmapData(this._image.width, 
											this._image.height, true, 0x00FFFFFF);
			this._imageData.draw(this._image.content, this._image.transform.matrix, 
														null, null, null, true);
			this._imageBitmap = new Bitmap(this._imageData, "always", true);
			this._imageBitmap.x = (super.width - this._imageBitmap.width) >> 1;
			this._imageBitmap.y = (super.height - this._imageBitmap.height) >> 1;
			super.addChild(this._imageBitmap);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Private methods
		//
		//--------------------------------------------------------------------------
	}
	
}